package cn.qyd.javasynchronized;

/**
 * @author 邱运铎
 * @date 2024-03-14 22:06
 */
public class SynchronizedDemo01 implements Runnable{
    static int i;

    public synchronized void incrementI() {
        i++;
    }
    @Override
    public void run() {
        for (int j = 0; j < 1000; j++) {
            incrementI();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        SynchronizedDemo01 synchronizedDemo01 = new SynchronizedDemo01();
//        Thread thread1 = new Thread(synchronizedDemo01);
//        Thread thread2 = new Thread(synchronizedDemo01);
//
//        thread1.start();
//        thread2.start();
//
//        thread1.join();
//        thread2.join();

        Thread thread1 = new Thread(new SynchronizedDemo01());
        Thread thread2 = new Thread(new SynchronizedDemo01());

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("i result is : " + i);
    }
}
